home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / main.c < prev    next >
C/C++ Source or Header  |  1990-07-08  |  4KB  |  168 lines

  1. /*
  2. **    file:        main.c
  3. **    purpose:    Top level entry point of bison
  4.  
  5. Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  6.  
  7. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY.  No author or distributor accepts responsibility to anyone
  9. for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.
  11. Refer to the BISON General Public License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute BISON,
  14. but only under the conditions described in the BISON General Public
  15. License.  A copy of this license is supposed to have been given to you
  16. along with BISON so you can know your rights and responsibilities.  It
  17. should be in a file named COPYING.  Among other things, the copyright
  18. notice and this notice must be preserved on all copies.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. #include "machine.h"    /* JF for MAXSHORT */
  29. #include "getargs.h"
  30. #include "files.h"
  31. #include "symtab.h"
  32. #include "state.h"
  33. #include "reader.h"
  34. #include "nullable.h"
  35. #include "lalr.h"
  36. #include "lr0.h"
  37. #include "print.h"
  38. #include "conflict.h"
  39. #include "derives.h"
  40. #include "output.h"
  41. #include "bison.h"
  42.  
  43. extern  int lineno;
  44. extern  int verboseflag;
  45.  
  46. static void usage( void) ;
  47.  
  48. /* Nonzero means failure has been detected; don't write a parser file.  */
  49. int failure;
  50.  
  51. int main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.   failure = 0;
  56.   lineno = 0;
  57.  
  58.   if (argc < 2)
  59.     usage() ;
  60.  
  61.   getargs(argc, argv);
  62.   openfiles();
  63.  
  64.   /* read the input.  Copy some parts of it to fguard, faction, ftable and fattrs.
  65.      In file reader.
  66.      The other parts are recorded in the grammar; see gram.h.  */
  67.   reader();
  68.  
  69.   /* record other info about the grammar.  In files derives and nullable.  */
  70.   set_derives();
  71.   set_nullable();
  72.  
  73.   /* convert to nondeterministic finite state machine.  In file LR0.
  74.      See state.h for more info.  */
  75.   generate_states();
  76.  
  77.   /* make it deterministic.  In file lalr.  */
  78.   lalr();
  79.  
  80.   /* Find and record any conflicts: places where one token of lookahead is not
  81.      enough to disambiguate the parsing.  In file conflicts.
  82.      Currently this does not do anything to resolve them;
  83.      the trivial form of conflict resolution that exists is done in output.  */
  84.   initialize_conflicts();
  85.  
  86.   /* print information about results, if requested.  In file print. */
  87.   if (verboseflag)
  88.     verbose();
  89.   else
  90.     terse();
  91.  
  92.   /* output the tables and the parser to ftable.  In file output. */
  93.   output();
  94.  
  95.   done(failure);
  96.   return 0 ;
  97. }
  98.  
  99. /* functions to report errors which prevent a parser from being generated */
  100.  
  101. void fatal(s)
  102. char *s;
  103. {
  104.   extern char *infile;
  105.  
  106.   if (infile == 0)
  107.     fprintf(stderr, "fatal error: %s\n", s);
  108.   else
  109.     fprintf(stderr, "\"%s\", line %d: %s\n", infile, lineno, s);
  110.   done(1);
  111. }
  112.  
  113.  
  114. /* JF changed to accept/deal with variable args.  Is a real kludge since
  115.    we don't support _doprnt calls */
  116. /*VARARGS1*/
  117. void fatals(fmt,x1,x2,x3,x4,x5,x6,x7,x8)
  118. char *fmt;
  119. int x1,x2,x3,x4,x5,x6,x7,x8 ;
  120. {
  121.   char buffer[200];
  122.  
  123.   sprintf(buffer, fmt, x1,x2,x3,x4,x5,x6,x7,x8);
  124.   fatal(buffer);
  125. }
  126.  
  127.  
  128.  
  129. void toomany(s)
  130. char *s;
  131. {
  132.   char buffer[200];
  133.  
  134.     /* JF new msg */
  135.   sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s);
  136.   fatal(buffer);
  137. }
  138.  
  139.  
  140.  
  141. void berror(s)
  142. char *s;
  143. {
  144.   fprintf(stderr, "internal error, %s\n", s);
  145.   abort();
  146. }
  147.  
  148. static void usage( void)
  149. {
  150.  int i ;
  151.  static char *s[9] = {
  152.     "BISON\t\t\t\t(Jan. 7, 1989)",
  153.     "\tusage:\tbison -{dlvty} <infile.Y> [-o <outfile.ext>]",
  154.     "\t-d\tgenerate token definition file",
  155.     "\t-l\tfilter #line directives from output file",
  156.     "\t-v\tVerbose.  generate state machine statistics file",
  157.     "\t-t\tdebugging Trace enabled",
  158.     "\t-y\tYacc compatibility. produce files named YTAB.?",
  159.     "\t-o\tOutput filename",
  160.     "\0"
  161.     } ;
  162.  
  163.     for (i=0 ; i<8 ; i++)
  164.         fprintf( stderr,"%s\n", s[i]) ;
  165.  
  166.     exit(0) ;
  167. }
  168.